package com.example.app2;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    Button search, delete;
    TextView tv;
    EditText ed;
    ContentResolver contentResolver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        contentResolver = this.getContentResolver();

        search = findViewById(R.id.search);
        delete = findViewById(R.id.delete);
        tv = findViewById(R.id.tv);
        ed = findViewById(R.id.ed);

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.example.exp_7.myprovider/search");
                Cursor cursor = contentResolver.query(uri, new String[]{"id","name","age","height"}, "id = ?", new String[]{ed.getText().toString()}, null);
                if (cursor.moveToNext()) {
                    String id = cursor.getString(0);
                    String name = cursor.getString(1);
                    String age = cursor.getString(2);
                    String height = cursor.getString(3);
                    tv.setText("id:"+id+","+"name:" + name + "," + "age:" + age + "," + "height:" + height);
                    //Toast.makeText(getApplicationContext(),cursor.toString() , Toast.LENGTH_SHORT).show();
                    Toast.makeText(getApplicationContext(),"远程查询成功" , Toast.LENGTH_SHORT).show();
                    ed.setText("");
                } else {
                    Toast.makeText(getApplicationContext(), "远程查询失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.example.exp_7.myprovider/delete");
                contentResolver.delete(uri, "id = ?", new String[]{ed.getText().toString()});
                Toast.makeText(getApplicationContext(), "远程删除成功", Toast.LENGTH_SHORT).show();
                ed.setText("");
            }
        });
    }
}